package com.ruowei.modules.foodOrder.service;

import com.ruowei.modules.foodOrder.repository.ShoppingCartRepository;
import com.ruowei.modules.sys.utils.AppletTokenUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class ShoppingCartService {
    private final ShoppingCartRepository shoppingCartRepository;

    public ShoppingCartService(ShoppingCartRepository shoppingCartRepository) {
        this.shoppingCartRepository = shoppingCartRepository;
    }

    public void clearShoppingCart(String merchantCode, HttpServletRequest request) {
        if (com.ruowei.common.lang.StringUtils.isEmpty(merchantCode)) {
            shoppingCartRepository.findAllByMemberCode(AppletTokenUtils.getOpenIdByToken(request).getOpenId())
                .forEach(shoppingCartRepository::delete);
        } else {
            shoppingCartRepository.findAllByMemberCodeAndMerchantCode(AppletTokenUtils.getOpenIdByToken(request).getOpenId(), merchantCode)
                .forEach(shoppingCartRepository::delete);
        }
    }
}
